//
//  KWebViewController.m
//  NewPro
//
//  Created by 何荧红 on 2018/10/25.
//  Copyright © 2018 何荧红. All rights reserved.
//

#import "KWebViewController.h"
#import "KnowledgeList.h"
#import "KnowledgeModel.h"
#import "kWebCell.h"

@interface KWebViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    
    CGFloat currentHeight;
}
@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,copy) NSArray *imgArr;

@end

@implementation KWebViewController

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.5)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    }
    
    return _tableView;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    
    return currentHeight;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.imgArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    kWebCell *cell = [kWebCell tempWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.imgUrl = self.imgArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self initCSS];
    
    [self setNavBarWithTitle:[KnowledgeList sharedInstance].currentModel.coursename];
    
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
        
    }];
    
    [self initData];
    
}

- (void)initData{
    
    NSString *url = [KnowledgeList sharedInstance].currentModel.url;
    NSArray *arr = [url componentsSeparatedByString:@"/"];
    NSString *numStr = arr.lastObject;
    NSInteger num = [numStr integerValue];
    
    NSString *lastStr = [NSString stringWithFormat:@"/%@",numStr];
    NSRange range = [url rangeOfString:lastStr];//匹配得到的下标
    NSString *headerStr = [url substringToIndex:range.location];//截取范围内的字符串
    NSLog(@"数量+++++%@",headerStr);
    
    NSMutableArray *imgArr = [NSMutableArray array];
    for (int i = 1; i <= num; i++) {

        NSString *a = [NSString stringWithFormat:@"%@/%d.png",headerStr,i];
        [imgArr addObject:a];
    }
    
    self.imgArr = imgArr;
    
    UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgArr.firstObject]]];
    
    currentHeight = 250;
    if (img) {
        currentHeight = ScreenWidth *(img.size.height / img.size.width);
    }
    
    [self.tableView reloadData];
    
}

@end
